package com.entrogy.service.impl;

import com.entrogy.bean.SchoolBus;
import com.entrogy.bean.SchoolBusTeacher;
import com.entrogy.dao.SchoolBusMapper;
import com.entrogy.dao.SchoolBusTeacherMapper;
import com.entrogy.service.SchoolBusService;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author 许红松
 * @date 2019/4/23 0023 上午 11:38
 *  校车管理service 接口实现类
 */
@Service(value="schoolBusService")
@Transactional
public class SchoolBusServiceImpl implements SchoolBusService {

    @Autowired
    private SchoolBusMapper schoolBusMapper;//校车管理dao接口
    @Autowired
    private SchoolBusTeacherMapper schoolBusTeacherMapper;//校车管理跟班老师dao接口

    /**
     *  新增学校校车信息 注意：要选择对应的学校来新增
     * @param schoolBus
     * @return int
     */
    @Override
    public int insertSchoolBus(SchoolBus schoolBus) {
        String nowTime=System.currentTimeMillis()+"";
        schoolBus.setSchoolbusTeacher_id(nowTime);
        int m=schoolBusMapper.insertSchoolBus(schoolBus);
        if (m==1){
            //新增的根本教师 批量插入
            if (schoolBus.getSid()!=null && !"".equals(schoolBus.getSid())){
                insertTeachers(schoolBus);
            }

        }
        return 1;
    }

    /**
     *  根据校车id删除校车信息
     * @param schoolbus_id
     * @return int
     */
    @Transactional
    @Override
    public int deleteSchoolBus(String schoolbus_id) {
        return schoolBusMapper.deleteSchoolBus(schoolbus_id);
    }

    /**
     * 根据条件获取学校校车信息  （一辆校车对应一个或者多个老师）
     * @param map
     * @return String
     */
    @Override
    public List<Map<String,Object>> GetSchoolBusIdByT(Map<String, Object> map) {
        return schoolBusMapper.GetSchoolBusIdByT(map);
    }

    /**
     * 根据校车id获取校车信息
     * @param schoolbus_id
     * @return SchoolBus
     */
    @Override
    public SchoolBus GetSchoolBusLine(String schoolbus_id) {
        return schoolBusMapper.GetSchoolBusLine(schoolbus_id);
    }



    /**
     *    修改校车线路信息 是否成功  带入学校id
     * @param schoolBus
     * @return boolean
     */
    @Override
    public boolean UpdateSchoolBus(SchoolBus schoolBus) {
          String  SchoolbusTeacher_id=schoolBus.getSchoolbusTeacher_id();
          String Sid=schoolBus.getSid();
        if(SchoolbusTeacher_id!=null && !"".equals(SchoolbusTeacher_id)){
            List<String> list=schoolBusTeacherMapper.selectTeacherByTid(SchoolbusTeacher_id);
            String m= Joiner.on(",").join(list);
            if(Sid!=null && !"".equals(Sid)){
                String t = Sid.substring(0,Sid.length() - 1);
            if (m.equals(t)){//数据库中存在的跟班老师与相同 没必要修改
               schoolBus.setSchoolbusTeacher_id("");
             }else {
                int code=schoolBusTeacherMapper.deleteSchoolBusTeacher(SchoolbusTeacher_id);
                 if (code==1){
                     insertTeachers(schoolBus);
                 }
            }
                schoolBusMapper.UpdateSchoolBus(schoolBus);
            }
              return true;
        }else{
            String nowTime=System.currentTimeMillis()+"";
            schoolBus.setSchoolbusTeacher_id(nowTime);
            insertTeachers(schoolBus);
            return schoolBusMapper.UpdateSchoolBus(schoolBus);
        }

    }

    /**
     *   公用添加方法
     * @param schoolBus
     */
    public void  insertTeachers(SchoolBus schoolBus){
        String nowTime=System.currentTimeMillis()+"";
        String[] Teacherarry=schoolBus.getSid().split(",");
        int TeacherSize=Teacherarry.length;
        for (int i=0;i<TeacherSize;i++){
            SchoolBusTeacher schoolBusTeacher=new  SchoolBusTeacher();
            schoolBusTeacher.setBusid(schoolBus.getSchoolbus_id());
            schoolBusTeacher.setSchool_code(schoolBus.getSchool_code());
            schoolBusTeacher.setTeacherid(Teacherarry[i]);
            schoolBusTeacher.setSchoolbusTeacherId(nowTime);
            schoolBusTeacherMapper.insertSchoolBusTeacher(schoolBusTeacher);
        }
    }
    /**
     *   根据busid 获取学生list集合
     * @param map（busId）
     * @return list
     */
    @Override
    public List<Map<String, Object>> GetStudentsOfBus(Map<String, Object> map) {
        return schoolBusMapper.GetStudentsOfBus(map);
    }

    /**
     *  根据busid 和 tablenmae 查询学生集合
     * @param busId
     * @param tableName
     * @return map
     */
    @Override
    public Map<String, Object> getStudentListByID(String busId, String tableName) {
        return schoolBusMapper.getStudentListByID(busId,tableName);
    }
    /**
     *   查询数据总数
     * @param map
     * @return int
     */
    @Override
    public int total(Map<String, Object> map) {
        List<Map<String,Object>> list=schoolBusMapper.total(map);
        int m=list.size();
        return m;
    }

    /**
     *    查询教师信息
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getUserListByT(Map<String, Object> map) {
        return schoolBusMapper.getUserListByT(map);
    }

    /**
     *   查询教师总数
     * @param map
     * @return
     */
    @Override
    public int getUserListTotal(Map<String, Object> map) {
        return schoolBusMapper.getUserListTotal(map);
    }
}
